﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

using JohnnySins.DAL;
using JohnnySins.DAL.Repositories;
using JohnnySins.Entities;
using JohnnySins.Code;

namespace JohnnySins
{
    public partial class MyPhotosets : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadData();
            }
        }

        public void LoadData()
        {
            using (var repo = RepositoryFactory.ResolveRepository<PhotoSetRepository>())
            {
                List<PhotoSet> ps = repo.FindActivePublic().ToList();

                if (ps.Count == 0)
                    return;

                if (PhotoSetId == -1)
                    PhotoSetId = ps[0].PhotoSetId;

                List<PhotoSet> psCurrent = new List<PhotoSet>();
                psCurrent.Add(ps.Find(p => p.PhotoSetId == PhotoSetId));
                ps.Remove(ps.Find(p => p.PhotoSetId == PhotoSetId));

                rptMaster.DataSource = psCurrent;
                rptMaster.DataBind();

                if (ps.Count > 0)
                {
                    pnlOther.Visible = true;
                    LastPhotoSetId = ps.Last().PhotoSetId;
                    rptOther.DataSource = ps;
                    rptOther.DataBind();
                }
                else
                    pnlOther.Visible = false;
            }
        }

        protected void rptMaster_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                PhotoSet rowView = e.Item.DataItem as PhotoSet;

                Label lbl = e.Item.FindControl("lblPhotoSetDate") as Label;
                if (rowView.PhotoSetDate.HasValue)
                    lbl.Text = rowView.PhotoSetDate.Value.ToString("MMMM dd, yyyy");

                lbl = e.Item.FindControl("lblPhotoCount") as Label;
                lbl.Text = rowView.Photos.Count().ToString();

                Repeater rptDetail = e.Item.FindControl("rptDetail") as Repeater;
                rptDetail.DataSource = rowView.Photos;
                rptDetail.DataBind();
            }
        }

        protected void rptDetail_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                PhotoSetPhoto rowView = e.Item.DataItem as PhotoSetPhoto;

                HtmlImage img = e.Item.FindControl("imgPhoto") as HtmlImage;
                img.Src = "~/Files/PhotoSets/" + rowView.PhotoSetId.ToString() + "/" + rowView.PhysFileName;
                img.Attributes.Add("onclick", string.Format("showImageViewDialog('{0}')", ResolveClientUrl(img.Src)));
            }
        }

        protected void rptOther_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                PhotoSet rowView = e.Item.DataItem as PhotoSet;

                HyperLink hl = e.Item.FindControl("hlPhotoset") as HyperLink;
                hl.NavigateUrl = "~/MyPhotosets.aspx?psid=" + rowView.PhotoSetId.ToString();
                hl.Text = rowView.PhotoSetTitle;
                if (rowView.PhotoSetId != LastPhotoSetId)
                    hl.Text += ",";
            }
        }

        private int PhotoSetId
        {
            get
            {
                if (ViewState["PhotoSetId"] == null)
                {
                    int id = -1;

                    if (Request.QueryString["psid"] == null)
                        return -1;

                    if (!string.IsNullOrEmpty(Request.QueryString["psid"].ToString()))
                    {
                        if (int.TryParse(Request.QueryString["psid"].ToString(), out id))
                            return id;
                        else
                            return -1;
                    }
                    else
                        return -1;
                }
                else
                    return Convert.ToInt32(ViewState["PhotoSetId"]);
            }
            set
            {
                ViewState["PhotoSetId"] = value;
            }
        }

        private int LastPhotoSetId
        {
            get
            {
                if (ViewState["LastPhotoSetId"] == null)
                {
                    return -1;
                }
                else
                    return Convert.ToInt32(ViewState["LastPhotoSetId"]);
            }
            set
            {
                ViewState["LastPhotoSetId"] = value;
            }
        }
    }
}